package com.ftg.learn.demo;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 发布订阅模块发送者端
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/6/2 下午2:18
 */
public class PubSubSending {

    public static void main(String[] args) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("kjx");
        factory.setPassword("kjx");
        factory.setVirtualHost("/kjx");

        try (
                Connection connection = factory.newConnection();
                Channel channel = connection.createChannel()
        ) {
            String exname = "telmess";
            //声明一个交换器(交易所) 绑定类型
            channel.exchangeDeclare(exname,"fanout");

            String message = "ni hao ma";
            channel.basicPublish(exname, "", null, message.getBytes());
            System.out.println(" [x] Sent '" + message + "'");
        } catch (IOException | TimeoutException e) {
            throw new RuntimeException(e);
        }

    }

}
